/**
 * Created by hanpd on 2017/5/4.
 */

import CryptoUtils from '../utils/cryption/CryptoUtils';
import CaHelper from './CaHelper';

/**
 * 需要加密的字段
 * @type {[*]}
 */
const NeedEncodeFields = [
    "password",//密码
    "payeeIdentity",//收款人身份证号码
    "payeeInidcardValid",//证件有效期
    "storeManIdentity",//经营者身份证号码
    "storeManIdentityValid",//证件有效期
    "certCode",//Pos经营者身份证号
    "bkAcId",//Pos银行账户
    "idNoAuth",//Pos结算人身份证号
    "cardNoCipher",//Pos信用卡卡号
    "CVV2",//Pos CVV2
    "endDate"//Pos信用卡有效期
];

/**
 * 加密敏感字段
 * @param params
 */
export function encryptFields(params) {
    for (let key of Object.keys(params)) {
        if (NeedEncodeFields.indexOf(key) !== -1) {
            let value = params[key];
            if (value) {
                params[key] = CryptoUtils.rsaEncrypt(value, CaHelper.getDataPublicKey());
            }
        }
    }
}

/**
 * 加密通信body，返回"密文|key"
 * @param body
 * @returns {string}
 */
export function encryptBody(body) {
    let passPhrase = CryptoUtils.getPassPhrase(16);
    var encrypt = CryptoUtils.aesEncrypt(passPhrase, body);
    return [encrypt , passPhrase];
}

/**
 * 解密response body
 * @param passPhrase
 * @param body
 */
export function decryptBody(passPhrase, body) {
    return CryptoUtils.aesDecrypt(passPhrase, body);
}